javascript:(function(){

    /*
     * Helper functions
     */

    function injectJavaScriptFile( document, url, callback )
    {
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.src = url;

        /* Attach handlers for all browsers */
        var done = false;
        script.onload = script.onreadystatechange = function()
        {
                if( !done && ( !this.readyState 
                               || this.readyState == 'loaded' 
                               || this.readyState == 'complete') )
                {
                        done = true;

                        /* Call a callback */
                        if (callback)
                            callback();

                        /* Handle memory leak in IE */
                        script.onload = script.onreadystatechange = null;
                        head.removeChild( script );
                }
        };

        head.appendChild(script);
    }

    function injectYmacsContainer( parent )
    {
        var newDiv = document.createElement('div');
        newDiv.id = 'ymacs_container';
        parent.appendChild(newDiv);

        /* Show 'Loading...' message */
        var newDiv = document.createElement('div');
        newDiv.id = 'ymacs_loading';
        newDiv.style.textAlign = 'center';
        newDiv.style.padding = '4em';
        newDiv.appendChild( document.createTextNode('Loading Ymacs...') );
        parent.appendChild(newDiv);
    }

    /*
     * Global variables
     */

    frame = document.getElementById('canvas_frame').contentWindow;
    doc = frame.document;
    textareaContainer = doc.getElementsByClassName('At')[0];
    textarea = textareaContainer.getElementsByTagName('textarea')[0]; /* trying not to use 
                                                                         obfuscated class 
                                                                         names where possible */
    textMenu = doc.getElementsByClassName('eJ')[0];

    /* Make these variables available in frame context */
    frame.textareaContainer = textareaContainer;
    frame.textarea = textarea;
    frame.textMenu = textMenu;


    /*
     * Ymacs injection code
     */

    /* hide original textarea */
    textarea.style.position = 'absolute';
    textarea.style.left = '-3000px';

    /* hide original spellchecking menu */
    textMenu.style.display = 'none';

    injectYmacsContainer(textareaContainer);

    injectJavaScriptFile(doc, 'http://ymacs-for-gmail.googlecode.com/files/ymacs-for-gmail-0.1.js');
})();
